package com.surenpi.jenkins.client.credential;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;

public class CertificateCredential extends Credential {
   public static final String TYPENAME = "Certificate";
   private static final String BASECLASS = "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl";
   private static final String FILE_ON_MASTER_KEYSTORE_SOURCE_CLASS = "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl$FileOnMasterKeyStoreSource";
   private static final String UPLOAD_KEYSTORE_SOURCE_CLASS = "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl$UploadedKeyStoreSource";
   private String password;
   private String certificatePath;
   private byte[] certificateContent;
   private CERTIFICATE_SOURCE_TYPES certificateSourceType;

   public CertificateCredential() {
      this.setTypeName("Certificate");
   }

   public String getPassword() {
      return this.password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getCertificatePath() {
      return this.certificatePath;
   }

   public void setCertificatePath(String certificatePath) {
      this.certificatePath = certificatePath;
   }

   public byte[] getCertificateContent() {
      return this.certificateContent;
   }

   public void setCertificateContent(byte[] certificateContent) {
      this.certificateContent = certificateContent;
   }

   public CERTIFICATE_SOURCE_TYPES getCertificateSourceType() {
      return this.certificateSourceType;
   }

   public void setCertificateSourceType(CERTIFICATE_SOURCE_TYPES certificateSourceType) {
      this.certificateSourceType = certificateSourceType;
   }

   public Map<String, Object> dataForCreate() {
      Map<String, String> certificateSourceMap = new HashMap();
      certificateSourceMap.put("value", String.valueOf(this.getCertificateSourceType().getCertStoreType()));
      certificateSourceMap.put("stapler-class", this.getCertificateSourceType().getCertStoreClass());
      certificateSourceMap.put("$class", this.getCertificateSourceType().getCertStoreClass());
      if (this.getCertificateSourceType() == CertificateCredential.CERTIFICATE_SOURCE_TYPES.FILE_ON_MASTER) {
         certificateSourceMap.put("keyStoreFile", this.getCertificatePath());
      } else if (this.getCertificateSourceType() == CertificateCredential.CERTIFICATE_SOURCE_TYPES.UPLOAD_CERT_FILE) {
         certificateSourceMap.put("uploadedKeystore", Base64.encodeBase64String(this.getCertificateContent()));
      }

      Map<String, Object> innerMap = new HashMap();
      innerMap.put("scope", "GLOBAL");
      innerMap.put("id", this.getId());
      innerMap.put("description", this.getDescription());
      innerMap.put("password", this.getPassword());
      innerMap.put("stapler-class", "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl");
      innerMap.put("$class", "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl");
      innerMap.put("keyStoreSource", certificateSourceMap);
      Map<String, Object> jsonData = new HashMap();
      jsonData.put("", "1");
      jsonData.put("credentials", innerMap);
      return jsonData;
   }

   public Map<String, Object> dataForUpdate() {
      Map<String, String> certificateSourceMap = new HashMap();
      certificateSourceMap.put("value", String.valueOf(this.getCertificateSourceType().getCertStoreType()));
      certificateSourceMap.put("stapler-class", this.getCertificateSourceType().getCertStoreClass());
      certificateSourceMap.put("$class", this.getCertificateSourceType().getCertStoreClass());
      if (this.getCertificateSourceType() == CertificateCredential.CERTIFICATE_SOURCE_TYPES.FILE_ON_MASTER) {
         certificateSourceMap.put("keyStoreFile", this.getCertificatePath());
      } else if (this.getCertificateSourceType() == CertificateCredential.CERTIFICATE_SOURCE_TYPES.UPLOAD_CERT_FILE) {
         certificateSourceMap.put("uploadedKeystore", Base64.encodeBase64String(this.getCertificateContent()));
      }

      Map<String, Object> jsonData = new HashMap();
      jsonData.put("scope", "GLOBAL");
      jsonData.put("id", this.getId());
      jsonData.put("description", this.getDescription());
      jsonData.put("password", this.getPassword());
      jsonData.put("stapler-class", "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl");
      jsonData.put("$class", "com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl");
      jsonData.put("keyStoreSource", certificateSourceMap);
      return jsonData;
   }

   public static enum CERTIFICATE_SOURCE_TYPES {
      FILE_ON_MASTER("com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl$FileOnMasterKeyStoreSource", 0),
      UPLOAD_CERT_FILE("com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl$UploadedKeyStoreSource", 1);

      private String certStoreClass;
      private int certStoreType;

      private CERTIFICATE_SOURCE_TYPES(String storeClass, int storeType) {
         this.certStoreClass = storeClass;
         this.certStoreType = storeType;
      }

      public String getCertStoreClass() {
         return this.certStoreClass;
      }

      public int getCertStoreType() {
         return this.certStoreType;
      }
   }
}
